<template>
  <el-dialog
    :close-on-click-modal="false"
    title="项目基本信息"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="工程名称" prop="projName">
            <el-input v-model="form.projName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="承建单位" prop="constructionUnit">
            <el-input v-model="form.constructionUnit" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="省份" prop="provinceName">
            <el-select v-model="form.provinceName" :disabled="disableForm" style="width: 100%;" placeholder="请选择省份" @change="provinceChange">
              <el-option v-for="item in provinceData" :key="item.name" :label="item.name" :value="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="城市" prop="cityName">
            <el-select v-model="form.cityName" :disabled="disableForm" style="width: 100%;" placeholder="请选择城市" @change="cityChange">
              <el-option v-for="item in cityData" :key="item.name" :label="item.name" :value="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目状态" prop="projStatus">
            <el-select v-model="form.projStatus" :disabled="disableForm" style="width: 100%;" placeholder="请选择项目状态">
              <el-option v-for="item in projectInfoProjStatus" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目等级" prop="projLevel">
            <el-input v-model="form.projLevel" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目类型" prop="projType">
            <el-select v-model="form.projType" :disabled="disableForm" style="width: 100%;" placeholder="请选择项目类型">
              <el-option v-for="item in projectInfoProjType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目部电话" prop="projDepartmentPhone">
            <el-input v-model="form.projDepartmentPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目部传真" prop="projDepartmentFax">
            <el-input v-model="form.projDepartmentFax" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目部通讯地址" prop="projDepartmentAddress">
            <el-input v-model="form.projDepartmentAddress" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目部邮编" prop="projDepartmentPostCode">
            <el-input v-model="form.projDepartmentPostCode" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">扩展信息1</div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="项目管理模式" prop="projMode">
            <el-select v-model="form.projMode" :disabled="disableForm" style="width: 100%;" placeholder="请选择项目管理模式">
              <el-option v-for="item in projectInfoProjMode" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目经理" prop="projManager">
            <el-input v-model="form.projManager" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目经理电话" prop="projManagerPhone">
            <el-input v-model="form.projManagerPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目经理资质" prop="projManagerQualification">
            <el-input v-model="form.projManagerQualification" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目总工" prop="projChiefEngineer">
            <el-input v-model="form.projChiefEngineer" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目总工电话" prop="projChiefEngineerPhone">
            <el-input v-model="form.projChiefEngineerPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业主单位" prop="ownerUnit">
            <el-input v-model="form.ownerUnit" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业主通讯地址" prop="ownerAddress">
            <el-input v-model="form.ownerAddress" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业主邮编" prop="ownerPostCode">
            <el-input v-model="form.ownerPostCode" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业主电话" prop="ownerPhone">
            <el-input v-model="form.ownerPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业主传真" prop="ownerFax">
            <el-input v-model="form.ownerFax" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设计单位" prop="designUnit">
            <el-input v-model="form.designUnit" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设计人" prop="designPerson">
            <el-input v-model="form.designPerson" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设计电话" prop="designPhone">
            <el-input v-model="form.designPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="设计传真" prop="designFax">
            <el-input v-model="form.designFax" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">扩展信息2</div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="监理单位" prop="supervisionUnit">
            <el-input v-model="form.supervisionUnit" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="监理人" prop="supervisionPerson">
            <el-input v-model="form.supervisionPerson" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="监理通讯地址" prop="supervisionAddress">
            <el-input v-model="form.supervisionAddress" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="监理邮编" prop="supervisionPostCode">
            <el-input v-model="form.supervisionPostCode" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="监理电话" prop="supervisionPhone">
            <el-input v-model="form.supervisionPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="监理传真" prop="supervisionFax">
            <el-input v-model="form.supervisionFax" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="中标资质" prop="bidQualification">
            <el-input v-model="form.bidQualification" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="立项日期" prop="projectDate">
            <el-date-picker v-model="form.projectDate" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划开始时间" prop="planStartTime">
            <el-date-picker v-model="form.planStartTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划完成时间" prop="planFinishTime">
            <el-date-picker v-model="form.planFinishTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实际开始时间" prop="realStartTime">
            <el-date-picker v-model="form.realStartTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="建筑里程" prop="constructionMileage">
            <el-input v-model="form.constructionMileage" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="建筑高度" prop="constructionHeight">
            <el-input v-model="form.constructionHeight" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="6">
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="概况" prop="summary">
            <el-input v-model="form.summary" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addProjectInfo, updateProjectInfo, getProjectInfoDetail } from '@/api/contractManage'
import { getAreaList } from '@/api/common'
import { projectInfoProjStatus, projectInfoProjType, projectInfoProjMode } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddProjectInfo',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      projectInfoProjStatus,
      projectInfoProjType,
      projectInfoProjMode,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        organization: '',
        constructionUnit: '',
        provinceCode: '',
        provinceName: '',
        cityCode: '',
        cityName: '',
        projName: '',
        projStatus: '',
        projLevel: '',
        projType: '',
        projDepartmentPhone: '',
        projDepartmentFax: '',
        projDepartmentAddress: '',
        projDepartmentPostCode: '',
        projMode: '',
        projManager: '',
        projManagerPhone: '',
        projManagerEducation: '',
        projManagerQualification: '',
        projChiefEngineer: '',
        projChiefEngineerPhone: '',
        ownerUnit: '',
        ownerAddress: '',
        ownerPostCode: '',
        ownerPhone: '',
        ownerFax: '',
        designUnit: '',
        designPerson: '',
        designPhone: '',
        designFax: '',
        supervisionUnit: '',
        supervisionPerson: '',
        supervisionAddress: '',
        supervisionPostCode: '',
        supervisionPhone: '',
        supervisionFax: '',
        bidQualification: '',
        projectDate: '',
        planStartTime: '',
        planFinishTime: '',
        realStartTime: '',
        constructionMileage: '',
        constructionHeight: '',
        description: '',
        summary: ''
      },
      fileList: [
      ],
      rules: {
        projName: [
          { required: true, message: '请输入工程名称', trigger: 'change' }
        ],
        projStatus: [
          { required: true, message: '请选择项目状态', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        this.form.provinceCode = selectedData.code
        this.form.provinceName = selectedData.name
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    cityChange(e) {
      const selectedData = this.cityData.filter(val => val.name === e)[0]
      this.form.cityCode = selectedData.code
      this.form.cityName = selectedData.name
      this.$set(this.form, this.form.city, e)
    },
    getDocDetail(id) {
      getProjectInfoDetail({ projectInfoId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            projectInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addProjectInfo : updateProjectInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
